package org.esprit.ocm.server.metier.ec2.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.esprit.ocm.client.rpc.ec2.ZoneService;
import org.esprit.ocm.dto.ec2.impl.AvailabilityZoneMappingDto;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;

import com.xerox.amazonws.ec2.AvailabilityZone;
import com.xerox.amazonws.ec2.Jec2;

public class ZoneServiceImpl extends BaseServiceImpl implements ZoneService {

	/**
	 * Lists available Availability zones.
	 * 
	 * @return Array of Availability zones.
	 */
	public List<AvailabilityZoneMappingDto> listAvailabilityZones(ServerDto server, AwsCredentialsDto credentials) {

		// initialize the interface
		Jec2 ec2 = this.initConnection(server, credentials);

		List<AvailabilityZone> result = new ArrayList<AvailabilityZone>();
		List<AvailabilityZoneMappingDto> resultMapping = new ArrayList<AvailabilityZoneMappingDto>();

		try {
			List<String> l = new ArrayList<String>();
			result = ec2.describeAvailabilityZones(l);
			for (AvailabilityZone from : result) {
				
				AvailabilityZoneMappingDto to = new AvailabilityZoneMappingDto();
				to.setName(from.getName());
				to.setState(from.getState());
				to.setRegionName(from.getRegionName());
				to.setMessages(from.getMessages());
				
				resultMapping.add(to);
				
			}
			return resultMapping;

		} catch (Exception ex) {
			Logger logger = Logger.getLogger(this.getClass().getName());
			logger.log(Level.SEVERE, "Listing availability zones failed: " + ex.getMessage());

			return null;
		}

	}
}
